package com.lyc.tool;

import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

public class Uploadword {

    public String update(MultipartFile source, HttpSession session) throws IOException {
        //文件的原始名
        String filename = source.getOriginalFilename();
        //定制全局唯一的命名
        String ext = FilenameUtils.getExtension(filename);
        String uniqueFilename = filename;
        System.out.println("唯一文件名" + uniqueFilename);
        String sqlpath = null;
        if (ext.equals("jpg")) {
            String realpath = session.getServletContext().getRealPath("static/image");
            System.out.println("realpath" + realpath);
            source.transferTo(new File(realpath + "\\" + uniqueFilename));
            sqlpath = "/static/image/" + uniqueFilename;
        } else if(ext.equals("png")){
            String realpath = session.getServletContext().getRealPath("static/music");
            System.out.println("realpath" + realpath);
            source.transferTo(new File(realpath + "\\" + uniqueFilename));
            sqlpath = "/static/image/" + uniqueFilename;
        } else{
            String realpath = session.getServletContext().getRealPath("static/video");
            System.out.println("realpath" + realpath);
            source.transferTo(new File(realpath + "\\" + uniqueFilename));
            sqlpath = "/static/image/" + uniqueFilename;
        }
        return sqlpath;
    }
}
